<script>
// import { emit } from 'vue'
import { useVModel } from '@vueuse/core'
export default {
    name: 'XtxCheckbox',
    props: {
        modelValue: {
            type: Boolean,
            default: false
        }
    },
    setup(props, { emit }) {
        // const checked = ref(false)
        const checked = useVModel(props, 'modelValue', emit)
        const changeChecked = () => {
            const newVal = !checked.value
            checked.value = newVal
            emit('change', newVal)
        }

        // watch(() => props.modelValue, () => {
        //     checked.value = props.modelValue
        // }, { immediate: true })
        return { checked, changeChecked }
    }
}
</script>

<template>
    <div class="xtx-checkbox" @click="changeChecked()">
        <i v-if="checked" class="iconfont icon-checked"></i>
        <i v-else class="iconfont icon-unchecked"></i>
        <span v-if="$slots.default">
            <slot />
        </span>
    </div>
</template>

<style scoped lang="less">
@import '@/assets/styles/variables.less';

.xtx-checkbox {
    display: inline-block;
    margin-right: 2px;

    .icon-checked {
        color: @xtxColor;

        ~span {
            color: @xtxColor;
        }
    }

    i {
        position: relative;
        top: 1px;
    }

    span {
        margin-left: 2px;
    }
}</style>